from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select


class Web:

    def __int__(self):
        """
        实例变量
        """
        self.driver: webdriver.Chrome = None

    def openbroswer(self, br="gc"):
        """
        打开浏览器
        :param br:浏览器类型。 gc:chrome; ff:firefox; eg:edge
        """
        if br == "ff":
            self.driver = webdriver.Firefox()
        elif br == "ie":
            self.driver = webdriver.IE()
        elif br == "eg":
            self.driver = webdriver.Edge()
        else:
            self.driver = webdriver.Chrome()
            self.driver.implicitly_wait(10)
            self.driver.maximize_window()

    def openurl(self, url):
        """
        打开被测网站
        """
        self.driver.get(url)

        # xpath css_selector  id
        # //*[@id="kw"]  //input[text()="aaa"]  //*[contains(@name, "su")]
        # /html/body/div/div/div[2]/input/a
        # [id="kw"]  #id

    def __find_ele(self, locator=''):
        """
        定位元素标签并返回
        :param locator:定位表达式
        :return:
        """
        if locator.startswith('/'):
            ele = self.driver.find_element(By.XPATH, locator)
        elif locator.startswith('[') or locator.startswith('#'):
            ele = self.driver.find_element(By.CSS_SELECTOR, locator)
        else:
            ele = self.driver.find_element(By.ID, locator)
        return ele

    def click(self,locator=''):
        """
        点击操作
        :param locator: 定位表达式
        :return:
        """
        ele = self.__find_ele(locator)
        ele.click()

    def input(self, locator='', text=''):
        """
        输入操作
        :param locator: 定位表达式
        :param text: 输入的数据
        :return:
        """
        ele = self.__find_ele(locator)
        ele.send_keys(text)

    def intoframe(self, locatr=''):
        """
        切入frame
        :param locatr:定位表达式
        :return:
        """
        ele = self.__find_ele(locatr)
        self.driver.swith_to.frame(ele)

    def outoframe(self):
        """切入frame"""
        self.driver.swith_to.default_content()

    def select(self, locator='', visible_text=''):
        """
        下拉框选择
        :param locator: 定位表达式
        :param visible_text: 选择的文本值
        """
        ele = self.__find_ele(locator)
        select = Select(ele)
        select.select_by_visible_text(visible_text)

    def switchwin(self, idx=-1):
        """
        切换浏览器窗口
        :param idx:窗口下标
        """
        handles = self.driver.window_handles
        self.driver.switch_to.window(handles[idx])

    def quit(self):
        """退出浏览器"""
        self.driver.quit()

    def refresh(self):
        """浏览器刷新"""
        self.driver.refresh()

    def gettext(self, locator=''):
        """
        获取指定元素标签的文本数据
        :param locator: 定位表达式
        :return: 文本值
        """
        ele = self.__find_ele(locator)
        text = ele.text
        return text

    def getattribute(self, locator='', attribute_name=''):
        """
        获取指定元素标签指定属性值
        :param locator: 定位表达式
        :param attribute_name: 属性名称
        :return:
        """
        ele = self.__find_ele(locator)
        attr_value = ele.get_attribute(attribute_name)
        return attr_value

    def gettile(self):
        """获取网页title信息"""
        title = self.driver.title
        return title

    def jsclick(self, locator=''):
        """js点击操作"""
        ele = self.__find_ele(locator)
        js = "arguments[0].click();" # arguments[0] 表示执行js语句时,传入第一个元素标签对象
        self.driver.execute_script(js, ele)

    def sleep(self, t='1'):
        t = int(t)
        sleep(t)

    def assert_contain(self, result='', expect=''):
        """断言实际结果中包含期望的字符串数据"""
        if result.__contains__(expect):
            print('PASS')
            return True
        else:
            print('FAIL')
            return False



